/********************************************************************
 * 	弹出信息框对象（支持单选框和选择框两种类型）
 *	支持事件：
 * 			CLICK_OK		针对复选框	点击 OK 按钮后触发
 * 			CLICK_CANCEL	针对复选框	点击 Cancel 按钮后触发
 * 			CLICK_CLOSE		针对信息框	点击 Close 按钮后触发
 ********************************************************************/	
package anshe.object
{
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.text.TextFormatAlign;
	import flash.utils.Timer;

	public class MessageBox extends Sprite
	{
		public static const CLICK_OK:String	= "okClick";			/** 点 OK 按钮 **/
		public static const CLICK_CANCEL:String = "cancelClick";	/** 点 Cancel 按钮 **/
		public static const CLICK_CLOSE:String	= "quitClick";		/** 点 Quit 按钮 **/
		
		public var mcHot:Sprite;			/** 热响应区块,用于档住鼠标事件 **/
		public var bgSprite:Sprite;			/** 背景对象,用于拖动处理 **/
		public var msgText:ExTextField;		/** 文字框,用于显示信息内容 **/
		public var msgTime:ExTextField;		/** 计时器对象 **/
		public var btnOK:SimpleButton;		/** OK 按钮 **/
		public var btnCancel:SimpleButton;	/** 取消 按钮 **/
		public var btnClose:SimpleButton;	/** 退出 按钮 **/		
		
		private var gTime:Timer;			/** 延时计时器对象 **/
		private var tmpData:Object;			/** 临时保存的数据 **/
		
		private static var msgBox:MessageBox;		/**	永久对象 **/
		
		public static function GetBox(isSelect:Boolean = false):MessageBox
		{
			if(!msgBox)
			{
				msgBox = new MessageBox(isSelect);
			}
			return msgBox;
		}
		
		public function MessageBox(isSelect:Boolean = false, numLast:Number = 0)
		{
			this.cacheAsBitmap = true;
			
			InitObject(isSelect);
			InitTime(numLast);
			InitListener();
		}
		
		/** 显示对象 **/
		public function Show():void
		{
			mcHot.graphics.beginFill(0xFFFFFF, 0);
			mcHot.graphics.drawRect(-3000, -3000, 6000, 6000);
			mcHot.graphics.endFill();
		}
		
		/** 隐藏对象 **/
		public function Hidden():void
		{
			if(this.mcHot)
			{
				mcHot.graphics.clear();
			}
			
			if(this.parent)
			{
				this.parent.removeChild(this);
			}
		}
		
		/** 重置 X Y 位置 **/
		public function SetPlace(xx:Number, yy:Number):void
		{
			this.x = xx;
			this.y = yy;
		}
		
		/** 设置消息内容 **/
		public function set msg(str:String):void
		{
			this.msgText.text = str;
			// this.msgText.isBord = true;
			
			if(this.msgTime)
			{
				if(!this.msgTime.visible)
				{
					this.msgText.y = (this.bgSprite.height - this.msgText.height - this.msgText.y) / 2;
				}
			}
		}
		
		/** 保存临时数据 **/
		public function set data(obj:Object):void
		{
			tmpData = obj;
		}
		
		/** 返回临时数据 **/
		public function get data():Object
		{
			return tmpData;
		}
		
		public function get objWidth():Number
		{
			return this.bgSprite.width;
		}
		
		public function get objHeight():Number
		{
			return this.bgSprite.height
		}

		/*****************************************************************/

		/** 初始化对象及变量 **/
		private function InitObject(flag:Boolean):void
		{
			if(flag)
			{
				btnOK.visible = true;
				btnOK.x = this.btnCancel.x - btnOK.width - 10;
				btnCancel.visible = true;				
			}
			else
			{
				btnOK.visible = true;
				btnOK.x = this.btnCancel.x - btnOK.width/2;
				btnCancel.visible = false;				
			}
			
			tmpData = new Object();			
			
			msgText.SetSize(this.bgSprite.width * 0.8, this.bgSprite.height * 0.6);
			// msgText.color = 0x00CCFF;
			
			if(this.msgTime)
			{
				msgTime.SetSize(360, 20);
				msgTime.color = 0xFFCC00;
				msgTime.isBord	= false;
				msgTime.size		= 16;
				msgTime.align		= TextFormatAlign.CENTER;
				msgTime.text		= "";
				msgTime.mouseEnabled = msgTime.mouseChildren = false;
			}
			
			msgText.isBord	= false;
			msgText.size	= 16;
			msgText.align	= TextFormatAlign.CENTER;
			msgText.text 	= "";
			msgText.mouseEnabled = msgText.mouseChildren = false;
		}
		
		private var numLast:Number;		/** 隐藏对话框的时间 **/
		
		private function InitTime(num:Number = 0):void
		{
			numLast = num;
			
			if(this.msgTime)
			{
				if(numLast == 0)
				{
					this.msgTime.visible = false;
				}
				else
				{
					this.msgTime.visible = true;
					
					gTime = new Timer(1000, numLast);
					gTime.addEventListener(TimerEvent.TIMER, onTimeEvent);
					gTime.addEventListener(TimerEvent.TIMER_COMPLETE, onTimeDoneEvent);
					gTime.start();
				}
			}
		}
		
		private function onTimeEvent(e:TimerEvent):void
		{
			this.numLast --;
			if(this.msgTime)
			{
				this.msgTime.text = "last " + this.numLast + " times";
			}
		}
		
		private function onTimeDoneEvent(e:TimerEvent):void
		{
			this.StopTimerListener();
						
			this.dispatchEvent(new Event(MessageBox.CLICK_CLOSE));
			this.dispatchEvent(new Event(MessageBox.CLICK_CANCEL));
		}
		
		private function StopTimerListener():void
		{
			if(gTime)
			{
				gTime.stop();
				gTime.addEventListener(TimerEvent.TIMER, onTimeEvent);
				gTime.addEventListener(TimerEvent.TIMER_COMPLETE, onTimeDoneEvent);
			}
		}
		
		/** 设置事件侦听 **/
		private function InitListener():void
		{
			this.bgSprite.addEventListener(MouseEvent.MOUSE_DOWN, onStartDrag);
			this.bgSprite.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
			
			this.mcHot.addEventListener(MouseEvent.CLICK, onHotClick);
			
			this.btnClose.addEventListener(MouseEvent.CLICK, onCloseThis);
			this.btnOK.addEventListener(MouseEvent.CLICK, onOKClick);
			this.btnCancel.addEventListener(MouseEvent.CLICK, onCancelClick);
		}
		
		/** 开始拖动对象 **/
		private function onStartDrag(e:MouseEvent):void
		{
			this.startDrag();
			
			this.stage.addEventListener(MouseEvent.MOUSE_UP, onStopDrag);
		}
		
		/** 停止拖拖动对象 **/
		private function onStopDrag(e:MouseEvent):void
		{
			this.stopDrag()
			
			this.stage.removeEventListener(MouseEvent.MOUSE_UP, onStopDrag);
		}
		
		/** 点击 热响应区 什么事情也不做(主要用于屏蔽鼠标事件 **/
		private function onHotClick(e:MouseEvent):void
		{			
		}
		
		/** 点击 Close 按钮 **/
		private function onCloseThis(e:MouseEvent):void
		{
			this.StopTimerListener();
			this.dispatchEvent(new Event(MessageBox.CLICK_CLOSE));
		}
		
		/** 点击 OK 按钮 **/
		private function onOKClick(e:MouseEvent):void
		{
			this.StopTimerListener()
			if(this.btnCancel.visible)
			{
				this.dispatchEvent(new Event(MessageBox.CLICK_OK));
			}
			else
			{
				this.dispatchEvent(new Event(MessageBox.CLICK_CLOSE));
			}
		}

		/** 点击 Cancel 按钮 **/
		private function onCancelClick(e:MouseEvent):void
		{
			this.StopTimerListener();
			this.dispatchEvent(new Event(MessageBox.CLICK_CANCEL));
		}
	}
}